package com.netease.web.system;

import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.netease.domain.Commodity;
import com.netease.domain.PageInfo;
import com.netease.service.CommodityService;
import com.netease.service.ShoppingcartService;
import com.netease.service.UserCommodityService;

@Controller
public class SellerIndexController {
	
	@Autowired
	private CommodityService commodityService;
	
	@Autowired
	private UserCommodityService usercommodityService;
	
	@Autowired
	private ShoppingcartService shoppingcartService;

	@RequestMapping("seller/index/all")
	public String index_all(Model model, HttpServletRequest request) {
		String page = request.getParameter("page");
		page = StringUtils.defaultIfBlank(page, "1");
		int pageNumber = Integer.valueOf(page);
		
		int total = commodityService.findCount();
		PageInfo<Commodity> pageInfo = new PageInfo<>(total, pageNumber);
		pageInfo.setList(commodityService.findAllByPage(pageInfo.getPageStart()));
		
		model.addAttribute("page", pageInfo);
		return "seller_index";
	}
	
	
	@RequestMapping("seller/detail")
	public String detail(Model model, HttpServletRequest request) {
		String id = request.getParameter("id");
		id = StringUtils.defaultIfBlank(id, "1");
		Commodity c = commodityService.findOne(id);
		model.addAttribute("commodity", c);
		return "seller_detail";
	}
	
	
	@RequestMapping("seller/edit")
	public String edit(Model model, HttpServletRequest request) {
		String id = request.getParameter("id");
		id = StringUtils.defaultIfBlank(id, "1");
		Commodity c = commodityService.findOne(id);
		model.addAttribute("commodity", c);
		return "seller_edit";
	}
	
	@RequestMapping(value="seller/editSubmit", method=RequestMethod.POST)
	public String editSubmit(Model model, HttpServletRequest request) {
		String id = request.getParameter("id");
		id = StringUtils.defaultIfBlank(id, "1");
		String title = request.getParameter("title");
		String summary = request.getParameter("summary");
		String pic = request.getParameter("image");
		String content = request.getParameter("detail");
		int remainningAmount = Integer.valueOf(request.getParameter("remainningAmount"));
		Double price = Double.valueOf(request.getParameter("price"));
		
		Commodity c = commodityService.findOne(id);
		c.setContent(content);
		c.setPicture(pic);
		c.setPrice(price);
		c.setSummary(summary);
		c.setTitle(title);
		c.setRemainningAmount(remainningAmount);
		commodityService.update(c);
		
		return "redirect:/seller/detail?id="+id;
	}
	
	@RequestMapping("seller/public")
	public String seller_public() {
	
		return "seller_public";
	}
	
	
	@RequestMapping(value="seller/publicSubmit", method=RequestMethod.POST)
	public String publicSubmit(Model model, HttpServletRequest request) {
		
		String title = request.getParameter("title");
		String summary = request.getParameter("summary");
		String pic = request.getParameter("image");
		String content = request.getParameter("detail");
		Double price = Double.valueOf(request.getParameter("price"));
		int remainningAmount = Integer.valueOf(request.getParameter("remainningAmount"));
		
		Commodity c = new Commodity();
		String id = UUID.randomUUID().toString();
		c.setId(id);
		c.setContent(content);
		c.setPicture(pic);
		c.setPrice(price);
		c.setSummary(summary);
		c.setTitle(title);
		c.setSoldAmount(0);
		c.setRemainningAmount(remainningAmount);
		commodityService.save(c);
		
		return "redirect:/seller/detail?id="+id;
	}
	

}
